package controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import po.weiboUser;
import service.weibouserservice;

@Controller
public class weiboController {

	@Autowired
	private weibouserservice us;

	@RequestMapping("/weibozhuce")
	public String weibozhuce() {
		return "/html/zhuce.html";
	}

	@RequestMapping(value = "/insertuser", method = { RequestMethod.POST })
	@ResponseBody
	public String insertuser(String account, String password, String username, HttpServletRequest request) {
		weiboUser wbu = new weiboUser();
		wbu.setAccount(account);
		wbu.setPassword(password);
		wbu.setUsername(username);
		System.out.println(wbu);
		return us.insertuser(wbu);
	}

	/**
	 * 
	 * @作者:
	 * @简介:验证用户是否重复
	 * @param account
	 * @return
	 */
	@RequestMapping(value = "/isrepetition", method = RequestMethod.GET)
	@ResponseBody
	public String isrepetition(@RequestParam("account") String account) {
		weiboUser wbs = us.queryByAccount(account);
		if (wbs != null) {
			return "yes";
		} else {
			return "no";
		}
	}
	
	@RequestMapping(value="/userstate",method=RequestMethod.GET,produces = {"application/json;charset=UTF-8"})
	@ResponseBody
	public weiboUser userstate(HttpServletRequest request) {
		HttpSession hsc = request.getSession();
		weiboUser wbu = (weiboUser) hsc.getAttribute("logingUser");
		return wbu;
	}
	
	@RequestMapping(value="/login",method=RequestMethod.POST)
	@ResponseBody
	public String login(String account,String password ,HttpServletRequest request) {
		weiboUser wbu = us.queryByAccount(account);
		HttpSession session = request.getSession();
		
		
		if(wbu!=null && wbu.getPassword().equals(password)) {
			session.setAttribute("logingUser", wbu);
			return "success";
		}else {
			return "error";
		}
	}
	
	@RequestMapping(value="/gerenweibo")
	public String gerenweibo() {
		return "/html/geren.html";
	}
	
	
	@RequestMapping(value="/insertarticle",method=RequestMethod.POST)
	@ResponseBody
	public String publishform(String contenttext) {
		System.out.println(contenttext);
		return "scuuess";
	}
	
	
}
